package com.tuanzi.loan.business.service.xhh.settlementsystem.impl;

import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementCompany;
import com.tuanzi.loan.business.repository.xhh.settlementsystem.XhhSettlementCompanyRepository;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhSettlementCompanyService;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhSettlementIncomeActivityService;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhSettlementIncomeService;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhSettlementProductService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.xhh.settlementsystem.XhhSettlementCompanyRequest;
import com.tuanzi.loan.vo.response.xhh.settlementsystem.XhhSettlementCompanyResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author caiwanlin
 * @create 2018-08-16 20:03
 */
@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class XhhSettlementCompanyServiceImpl extends BaseAuditServiceImpl<XhhSettlementCompany,Long> implements XhhSettlementCompanyService {

    @Autowired
    private XhhSettlementIncomeActivityService xhhSettlementIncomeActivityService;

    @Autowired
    private XhhSettlementIncomeService xhhSettlementIncomeService;

    @Autowired
    private XhhSettlementProductService xhhSettlementProductService;

    @Autowired
    private XhhSettlementCompanyRepository xhhSettlementCompanyRepository;

    @Override
    public Page<XhhSettlementCompanyResponse> list(PageSearcher<XhhSettlementCompanyRequest> pageSearcher) {
        return xhhSettlementCompanyRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }

    @Override
    public List<XhhSettlementCompany> getCompanyList() {
        return xhhSettlementCompanyRepository.getCompanyList();
    }

    @Override
    public XhhSettlementCompany findByCompanyName(String companyName) {
        return xhhSettlementCompanyRepository.findByCompanyName(companyName);
    }

    @Override
    public void onDelete(Long id) {
        xhhSettlementIncomeActivityService.deleteByCompanyId(id);
        xhhSettlementIncomeService.deleteByCompanyId(id);
        xhhSettlementProductService.deleteByCompanyId(id);
        xhhSettlementCompanyRepository.delete(id);
    }

    @Override
    protected JPAAuditRepository<XhhSettlementCompany, Long> getRepository() {
        return xhhSettlementCompanyRepository;
    }
}
